package com.matrixxun.starry.badgetextview;

import com.matrixxun.starry.badgetextview.util.AttrUtil;
import com.matrixxun.starry.badgetextview.util.ShapeUtil;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

/**
 * Created by matrixxun on 2016/8/30.
 */
public class MaterialBadgeTextView extends Text implements Text.TextObserver {
    private static final int DEFAULT_FILL_TYPE = 0;
    private final String BG_COLOR = "background";
    private Color backgroundColor = new Color(0x999999FF);
    private static final float SHADOW_RADIUS = 1.5f;
    private static final float X_OFFSET = 0f;
    private static final float Y_OFFSET = 1.75f;
    private float density;
    private int mShadowRadius;
    private int shadowYOffset;
    private int shadowXOffset;
    private int basePadding;
    private int diffWH;
    private boolean isHighLightMode;
    AttrUtil attrUtils;

    public MaterialBadgeTextView(final Context context) {
        this(context, null);
    }

    public MaterialBadgeTextView(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public MaterialBadgeTextView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, null);
        init(context, attrs);
    }

    private void init(Context context, AttrSet attrs) {
        attrUtils = new AttrUtil(attrs);
        setTextAlignment(TextAlignment.CENTER);
        density = getContext().getResourceManager().getDeviceCapability().screenDensity / 160;
        mShadowRadius = (int) (density * SHADOW_RADIUS);
        shadowYOffset = (int) (density * Y_OFFSET);
        shadowXOffset = (int) (density * X_OFFSET);
        basePadding = (mShadowRadius * 2);
        float textHeight = getTextSize();
        float textWidth = textHeight / 4;
        diffWH = (int) (Math.abs(textHeight - textWidth) / 2);
        int horizontalPadding = basePadding + diffWH;
        setPadding(horizontalPadding, basePadding, horizontalPadding, basePadding);
        backgroundColor = attrUtils.getAttrColor(BG_COLOR, backgroundColor);
        addTextObserver(this);
        setLayoutRefreshedListener(new LayoutRefreshedListener() {
            @Override
            public void onRefreshed(Component component) {
                refreshBackgroundDrawable(component.getWidth(), component.getHeight());
            }
        });
    }

    @Override
    public void onTextUpdated(String text, int start, int lengthBefore, int lengthAfter) {
        // 纯色小红点模式下若有文本需要从无变为有, 要归位view的大小
        String strText = text == null ? "" : text.toString().trim();
        if (isHighLightMode && !"".equals(strText)) {
            ComponentContainer.LayoutConfig lp = getLayoutConfig();
            lp.height = ComponentContainer.LayoutConfig.MATCH_CONTENT;
            lp.width = ComponentContainer.LayoutConfig.MATCH_CONTENT;
            setLayoutConfig(lp);
            isHighLightMode = false;
        }
    }

    private void refreshBackgroundDrawable(int targetWidth, int targetHeight) {
        if (targetWidth <= 0 || targetHeight <= 0) {
            return;
        }
        CharSequence text = getText();
        if (text == null) {
            return;
        }
        if (text.length() == 1) { // 第一种背景是一个正圆形, 当文本为个位数字时
            ShapeElement circle;
            circle = new ShapeElement();
            circle.setShape(ShapeElement.OVAL);
            circle.setShaderType(ShapeElement.SWEEP_GRADIENT_SHADER_TYPE);
            circle.setRgbColor(new RgbColor(backgroundColor.getValue()));
            setBackground(circle);
        } else {
            // 第二种背景是上下两边为直线的椭圆, 当文本长度大于1时
            setBackground(ShapeUtil.createRectangleDrawable(
                    backgroundColor.getValue(),
                    getHeight() / 2,
                    getHeight() / 2,
                    getHeight() / 2,
                    getHeight() / 2));
        }
    }

    public void setBadgeCount(int count) {
        if (count > 0 && count <= 99) {
            setText(String.valueOf(count));
        } else if (count > 99) {
            setText("99+");
        } else {
            setText("0");
        }
    }

    public void setBackgroundColor(Color color) {
        backgroundColor = color;
        refresh();
    }

    private void refresh() {
        refreshBackgroundDrawable(getWidth(), getHeight());
    }
}

